package com.zenith.user.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.common.result.PageResult;
import com.zenith.user.po.domain.RolePermission;
import com.zenith.user.po.dto.RolePermissionDTO;
import com.zenith.user.service.RolePermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色权限关联管理
 *
 * @author makejava
 * @since 2025-08-15
 */
@Slf4j
@RestController
@RequestMapping("/api/user/rolePermission")
@Tag(name = "角色权限关联管理")
public class RolePermissionController {

    @Autowired
    private RolePermissionService rolePermissionService;

    @SecurePostMapping(value = "/addOrUpdateRolePermission", roles = {"ADMIN"}, permission = "rolePermission:write")
    @Operation(summary = "添加或修改角色权限关联")
    public Result<Void> saveOrUpdate(@RequestBody RolePermission entity) {
        log.info("添加或修改角色权限关联: {}", entity);
        rolePermissionService.saveOrUpdate(entity);
        return Result.success();
    }

    @SecurePostMapping(value = "/deleteRolePermissions", roles = {"ADMIN"}, permission = "rolePermission:delete")
    @Operation(summary = "批量删除角色权限关联")
    public Result<Void> deleteRolePermissions(@RequestBody List<String> ids) {
        log.info("批量删除角色权限关联: {}", ids);
        rolePermissionService.removeByIds(ids);
        return Result.success();
    }

    @SecurePostMapping(value = "/getRolePermissionById", roles = {"ADMIN", "USER"}, permission = "rolePermission:read")
    @Operation(summary = "根据ID查询角色权限关联")
    public Result<RolePermission> getRolePermissionById(@RequestParam String id) {
        log.info("根据ID查询角色权限关联: {}", id);
        return Result.success(rolePermissionService.getById(id));
    }

    @SecurePostMapping(value = "/getRolePermissionList", roles = {"ADMIN"}, permission = "rolePermission:read")
    @Operation(summary = "获取角色权限关联列表")
    public Result<List<RolePermission>> getRolePermissionList() {
        log.info("获取角色权限关联列表");
        return Result.success(rolePermissionService.list());
    }

    @SecurePostMapping(value = "/getRolePermissionPage", roles = {"ADMIN"}, permission = "rolePermission:read")
    @Operation(summary = "分页查询角色权限关联")
    public Result<PageResult<RolePermission>> getRolePermissionPage(RolePermissionDTO query) {
        log.info("分页查询角色权限关联: {}", query);
        return Result.success(rolePermissionService.getRolePermissionPage(query));
    }
}
